<?php

/**
 * @author Sujeet Gupta
 * @copyright 2014
 */

class PrivacySettings{
    function __construct() {
        
    }
    const PublicSetting=1;
    const DefaultSetting=1;
    const FriendsSetting=2;
    const SpecificFriendSetting=3;
    const ChooseClusterSetting=4;
}

class ReportAbuse{
    function __construct() {
        
    }
    public $Remove_Media_I_Dont_Like_This=2;
    public $Remove_Message_I_Dont_Like_This=3;
    public $Remove_Media_Its_Harrassing_Me=4;
    public $Remove_Message_Or_Speech_I_Dont_Like_This=5;
    public $I_Strongly_Report_For_This_DNA_Profile=6;
}
 
class PostStatus{
    /*1-9 reserved for report abuse cases */
    public $ReportAbuse="";
    public $ReportAbusetypes=""; //separated by comma
    public $IsHidden=0;
    public $Visible=1;
}

class PostType{
    function __construct() {
        
    }
    // derived from database table: af_user_posts_activities, field: Post_Type
    const Status_Updates=1;
    const Wall_Post=2;
    const Birthday=3;
    const Events=4;
    const Pages=5;
    const Blog=6;
    const Technopath=7;
    const Share=8;
    const Default_Type=2;
};


function responseWithJsonErrorCode($returnCode,$returnMsg)
{
    $jsonResult="{\"err\":".(string )$returnCode.",\"msg\":".$returnMsg."}";
    return $jsonResult;
}

function responseWithErrorCode($returnCode,$returnMsg)
{
    $jsonResult="{\"err\":".(string )$returnCode.",\"msg\":\"".$returnMsg."\"}";
    return $jsonResult;
}

class PostAction {
    function __construct() {
        
    }   
    public $Success=array("err_code"=>200,"msg"=>"Successfully posted");
    public $Failure=array("err_code"=>400,"msg"=>"Failed to post"); //BadRequest 
    public $Failed_For_Content=array("err_code"=>500,"msg"=>"No Data To Post"); //BadRequest
    public $Failed_For_Invalid_Session=array("err_code"=>501,"msg"=>"Invalid Session"); //BadRequest
    public $Failed_For_Invalid_Privacy_Settings=array("err_code"=>502,"msg"=>"Invalid Privacy Settings"); //BadRequest
};

class PrivacyAction {
    function __construct() {
        
    }   
    public $Success=array("err_code"=>200,"msg"=>"Successfully updated");
    public $Failure=array("err_code"=>400,"msg"=>"Failed to update");
};
/*
usage of enum

$actionRes = new PostAction(PostAction::Post); // or PostAction->Post
if ($actionRes == PostAction::Post) {
  // Do something
}
*/
class UserDetails{
    
    public $oLog;
    private $loggedInUserId=0;
    public $pdodbobj;
    public $connstatus=false;
    
    public $userId=0;
    public $emailId="";
    public $allyfolioUserID="";
    public $first_name="";
    public $last_name="";
    public $recovery_email="";
    public $address="";
    public $privacyType;
    public $latestUpdatedStatus="";
    public $lastLoginTime=0;
    public $currentTimeZone;
    public $dob;
    public $gender;
    public $profilePicPath="";
    public $profileLargePicPath="";
    public $isCelibrity=false;
    public $regDate=0;
    public $activatedDate=0;
    public $lastLoginFrom;
    public $profileName="";
    public $countryName="";
    public $countryId=0;
    public $feedsetting=0;
    public $lastupdatedfeedtimeread=0;
    public $topupdatedfeedtimestampread=0;
    
    //Notifications (on left navigations)
    public $latestFriendNotificationsArr;
    public $latestMessageNotificationsArr;
    public $latestEventsNotificationsArr;
    public $latestStatusNotificationsArr;
    
    function __construct() {
       try{
            $this->pdodbobj= new DB();
            $this->oLog= new Log();
            $this->connstatus=$this->pdodbobj->connect();
            
            $this->lastupdatedfeedtimeread=strtotime(date("Y-m-d H:i:s"));
            $this->topupdatedfeedtimestampread=$this->lastupdatedfeedtimeread;
            
            if(isset($_SESSION['Allyfolio_User_ID'])&& !empty($_SESSION['Allyfolio_User_ID']) && is_numeric($_SESSION['Allyfolio_User_ID'])){
                $this->loggedInUserId=trim($_SESSION['Allyfolio_User_ID']);    
            }
            else{
                die('UserDetails:__construct():: Invalid Session => '.$_SESSION['Allyfolio_User_ID'].' \t'. "\n");
            }
        }
        catch(Exception $ex){
            die('UserDetails:__construct():: \t'. $ex->getMessage(). "\n");   
        }
   }
    
    //get user details
    function getUserDetails($iUserId=0,$needUpdation=true){
        if(!is_numeric($iUserId) || $iUserId<=0){
            $this->userId=$this->loggedInUserId;
            if($needUpdation==true){
                $this->updateDetails($this->userId);
            }
        }
        else{
            $this->userId=$iUserId;
            $this->updateDetails($this->userId);
        }
        return $this;
    }
    
    function updateDetails($iUserId){
        try{
            $tempdetailsarr=$this->getUserBasicDetails($iUserId);
            $tempdetails=$tempdetailsarr[0];
            //print_r($tempdetailsarr);
                $fullpicpath="images/default_user_pic.jpg";
                $imgpath="images/default_user_pic.jpg";
                if(strstr($tempdetails["Profile_Pic_Path"],".")){
                    $imgpath=$tempdetails["Profile_Pic_Path"];
                }
                if(strstr($tempdetails["Large_Profile_Pic_Path"],".")){
                    $fullpicpath=$tempdetails["Large_Profile_Pic_Path"];
                }
                $this->profileName=$tempdetails["Full_Name"];
                $this->emailId=$tempdetails["Email_ID"];
                $this->allyfolioUserID=$tempdetails["Allyfolio_User_ID"];
                $this->dob=$tempdetails["DOB"];
                $this->gender=$tempdetails["Gender"];
                //$this->countryId=$tempdetails["Country_ID"];
                $countrydetailsarr=$this->getCountryDetails($this->countryId);
                $this->countryName=($countrydetailsarr[0]["Country_Name"])?$countrydetailsarr[0]["Country_Name"]:"";
                $this->profilePicPath=$imgpath;
                $this->profileLargePicPath=$fullpicpath;
                $this->isCelibrity=($tempdetails["Is_Celebrity"]==1)?true:false;
                $this->regDate=$tempdetails["Registration_Date"];
                $this->activatedDate=$tempdetails["Activation_Date"];
                $this->lastLoginFrom=$tempdetails["Status"];
                $this->lastLoginTime=$tempdetails["logtime"];
                $this->first_name=$tempdetails["First_Name"];
                $this->last_name=$tempdetails["Last_Name"];
                $this->recovery_email=$tempdetails["recovery_email"];
                $this->address=$tempdetails["Address"];
            
            //get privacy details
            $privacyTypearr=$this->getUserPrivacyDetails($iUserId);
            //print_r($privacyTypearr);
            //die("commonfeatures");
            $this->privacyType=$privacyTypearr;
            $this->feedsetting=($privacyTypearr["Default_Setting"]==0)?1:$privacyTypearr["Default_Setting"];
        }
        catch(Exception $ex){
            $actionRes['msg']=$ex->getMessage();
            die("CommonFeatures.php UserDetails:updateDetails():: \t".$ex->getMessage());
        }
    }
    //get privacy details
    public function getCountryDetails($iCountryId){        
        try{
            $postAction= new PostAction();
            if($this->pdodbobj!=null){// check if connection object is set to connection
                $selquery="select Country_Name,Short_Name as Country_Abbr_Name from af_countries  where Country_ID=:countryid";
                $existsrecord= $this->pdodbobj->fetch_array($this->pdodbobj->query($selquery,array(":countryid"=>$iCountryId)));
    
                if(count($existsrecord)>0)
                {
                     return $existsrecord;     
                }
            }
            else{
                $actionRes=$postAction->Failure;
                $actionRes["msg"]="Unable to connect to db";
            }
        }
        catch(Exception $ex){
            $actionRes['msg']=$ex->getMessage();
            die("CommonFeatures.php UserDetails:getCountryDetails():: \t".$ex->getMessage());
        }
    }
    //get privacy details
    public function getUserPrivacyDetails($iUserId){        
        try{
            $postAction= new PostAction();
            if($this->pdodbobj!=null){// check if connection object is set to connection
                //$this->pdodbobj->query("CALL getDefaultPrivacySettingForUser($iUserId,@defaultPrivacySetting)");
                $callresult=$this->pdodbobj->query("CALL getUserPrivacySettings($iUserId)");
                $existsrecord=$this->pdodbobj->fetch_array($callresult);
                //$existsrecord= $this->pdodbobj->fetch_array($this->pdodbobj->query("SELECT IFNULL(@defaultPrivacySetting,0) as Default_Setting"));
                //print_r($callresult);
                //print_r($existsrecord);
                //die("<br> iUserId:".$iUserId);
                if(count($existsrecord)>0)
                {
                     return $existsrecord[0];     
                }
            }
            else{
                $actionRes=$postAction->Failure;
                $actionRes["msg"]="Unable to connect to db";
            }
        }
        catch(Exception $ex){
            $actionRes['msg']=$ex->getMessage();
            die("CommonFeatures.php UserDetails:getUserPrivacyDetails():: \t".$ex->getMessage());
        }
    }
    
    //get privacy details
    public function getUserBasicDetails($iUserId){        
        try{
            $postAction= new PostAction();
            if($this->pdodbobj!=null){// check if connection object is set to connection
                $selquery="select 
                                uInfo.Email_ID,uInfo.First_Name,uInfo.Last_Name,uInfo.Allyfolio_User_ID, uInfo.Secondary_Email_ID as recovery_email,uInfo.Address, uInfo.DOB,uInfo.Full_Name,uInfo.Gender,uInfo.Profile_Pic_Path,uInfo.Large_Profile_Pic_Path,uInfo.Is_Celebrity,
                                uInfo.Is_Celebrity,uInfo.Registration_Date,uInfo.Activation_Date,
                                uLogInfo.Status,uLogInfo.logtime 
                            from 
                                af_users as uInfo 
                                
                                    join 
                                
                                (select 
                                        logtime,Status,User_ID 
                                    from 
                                        af_user_loginout_log 
                                    where 
                                        User_ID='".$iUserId."' 
                                    order by 
                                        ID desc limit 1)
                                         
                                 as uLogInfo 
                            where 
                                uInfo.User_ID=:userid
                                and uInfo.User_ID=uLogInfo.User_ID      
                            ";
                $existsrecord= $this->pdodbobj->fetch_array($this->pdodbobj->query($selquery,array(":userid"=>$iUserId)));
    
                if(count($existsrecord)>0)
                {
                     return $existsrecord;     
                }
            }
            else{
                $actionRes=$postAction->Failure;
                $actionRes["msg"]="Unable to connect to db";
            }
        }
        catch(Exception $ex){
            $actionRes['msg']=$ex->getMessage();
            die("CommonFeatures.php UserDetails:getUserBasicDetails():: \t".$ex->getMessage());
        }
    }
}
?>